﻿using DLL.NET.DJ.MSDM;

using DLL.Net60.MotionCtrl.ControlBase;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.NetStandard.FlushRobot.MS.Entity.Param;
using Google.Protobuf.WellKnownTypes;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Module.AutoRun.ViewModels
{
    public class PUC_MS_CleanViewModel : CPDMVVMBase, IRegionMemberLifetime
    {
        #region navigate

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        private IEventAggregator _eventAggregator;
        private IContainerExtension _container;
        private IRegionManager _regionManager;

        public override void Loaded()
        {
            wr_WorkPoint.ReadParam();
            wr_deviceConfig.ReadParam();
            wr_workConfig.ReadParam();
            ZAxisCount = wr_deviceConfig.Param.StationColNum;

            base.Loaded();
        }

        public override void UnLoaded()
        {
            base.UnLoaded();
        }

        #endregion navigate

        private IMotionCtrlBase _nMotionCtrl;

        private CMS_WR_WorkPointParam wr_WorkPoint = new CMS_WR_WorkPointParam();
        private CMS_WR_DeviceConfigParam wr_deviceConfig = new CMS_WR_DeviceConfigParam();
        private CMS_WR_WorkConfigParam wr_workConfig = new CMS_WR_WorkConfigParam();
        public IDJ_MS _msDJ;
        private int ZAxisCount = 0;

        public PUC_MS_CleanViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IContainerExtension container,
            IMotionCtrlBase _nMotionCtrlBase,
            IDJ_MS ms_dj
            ) : base(regionManager, eventAggregator)
        {
            _container = container;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _nMotionCtrl = _nMotionCtrlBase;
            _msDJ = ms_dj;
            Message = "";
        }

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            journal = navigationContext.NavigationService.Journal;
            GoForwardCommand.RaiseCanExecuteChanged();
            GoBackCommand.RaiseCanExecuteChanged();
            //return PageViews / 3 != 1;
            var obj = navigationContext.Parameters["CDJ"] as CDJ_MS;
            if (obj != null)
            {
                _msDJ = obj;
            }
        }

        public DelegateCommand<object> BtnClickCommand
        {
            get { return new DelegateCommand<object>(MoveBtnClick); }
        }

        private int CmdID = 0;

        private void MoveBtnClick(object button)
        {
            Button btn = (Button)button;
            string name = btn.Name;
            CmdID = Convert.ToInt32(name.Substring(3, 1));

            BtnIsEnabled = false;
            BtnIsClick = true;
            ThreadOver = false;
            //Thread thread = new Thread(ThreadBtnClick);
            //thread.SetApartmentState(ApartmentState.STA);
            //thread.Start();
            Task task1 = new Task(ThreadBtnClick);
            task1.Start();
            task1.Wait();
            BtnIsEnabled = true;
            BtnIsClick = false;
        }

        private void MoveXpos1()
        {
            MoveYPos1();
            _nMotionCtrl.AbsMove(0, wr_WorkPoint.Param.X_CleanPos1);
            _nMotionCtrl.WaitForStop(0);
        }

        private void MoveXpos2()
        {
            MoveYPos1();
            _nMotionCtrl.AbsMove(0, wr_WorkPoint.Param.X_CleanPos2);
            _nMotionCtrl.WaitForStop(0);
        }

        private void MoveXpos3()
        {
            MoveYPos1();
            _nMotionCtrl.AbsMove(0, wr_WorkPoint.Param.X_CleanPos3);
            _nMotionCtrl.WaitForStop(0);
        }

        private void MoveYPos1()
        {
            _nMotionCtrl.AbsMove(1, 0);
            _nMotionCtrl.WaitForStop(1);
        }

        private void MoveYPos2()
        {
            _nMotionCtrl.AbsMove(0, wr_WorkPoint.Param.X_CleanPos1);
            _nMotionCtrl.WaitForStop(0);
            _nMotionCtrl.AbsMove(1, wr_WorkPoint.Param.Y_CleanPos2);
            _nMotionCtrl.WaitForStop(1);
        }

        private void MoveZPos1()
        {
            for (int i = 2; i < (ZAxisCount + 2); i++)
            {
                _nMotionCtrl.AbsMove(i, 0);
            }
            _nMotionCtrl.WaitForStop();
        }

        private void MoveZPos2()
        {
            for (int i = 2; i < (ZAxisCount + 2); i++)
            {
                _nMotionCtrl.AbsMove(i, 40);
            }
            _nMotionCtrl.WaitForStop();
        }

        #region 定时器

        //按钮属性变量
        private bool _btnIsEnabled = true;

        public bool BtnIsEnabled
        {
            get { return _btnIsEnabled; }
            set
            {
                SetProperty(ref _btnIsEnabled, value);
                BtnClickCommand.RaiseCanExecuteChanged();
            }
        }

        private DispatcherTimer timer;
        private bool BtnIsClick = false;
        private bool ThreadOver = true;//复位结束标志位

        private void ThreadBtnClick()
        {
            try
            {
                switch (CmdID)
                {
                    case 0:
                        MoveXpos1();
                        break;

                    case 1:
                        MoveXpos2();
                        break;

                    case 2:
                        MoveXpos3();
                        break;

                    case 3:
                        MoveYPos1();
                        break;

                    case 4:
                        MoveYPos2();
                        break;

                    case 5:
                        MoveZPos1();
                        break;

                    case 6:
                        MoveZPos2();
                        break;

                    case 7:
                        if (_msDJ != null)
                        {
                            _msDJ.OpenAllJaw();
                        }
                        break;

                    case 8:
                        if (_msDJ != null)
                        {
                            _msDJ.CloseAllJaw(wr_workConfig.Param.djClosePosIndex);
                        }
                        break;

                    case 10:
                        {
                            if (_msDJ != null)
                            {
                                _msDJ.OpenJawHalf(0);
                            }
                        }
                        break;

                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            ThreadOver = true;
        }

        #endregion 定时器
    }
}